<?php
	function updateGoal($goalid, $goalname,$description,$startvalue,$currentvalue,$targetvalue,$priority,$measurename,$measuretype, $deadline){		
		deleteGoal($goalid);
		
		mysql_connect("localhost", "web964","pass17") or die ("Connection failed.");
		mysql_select_db("usr_web964_1") or die ("Database does not exist.");	
		

		$result = mysql_query(
		"INSERT INTO goal (name, description, startvalue, value, targetvalue, priority, measurename, fk_userid, fk_measuretypeid)
		 VALUES ('$goalname',
		 '$description',
		 '$startvalue',
		 '$currentvalue',
		 '$targetvalue',
		 '$priority',
		 '$measurename',
		 ".$_SESSION['userid'].",
		 ".$measuretype.")")
		 or die ("MySQL-Error: " . mysql_error());
		
		 $goalid = "";
		 $result2 = mysql_query(
		"SELECT id FROM  goal WHERE name = '$goalname'")
		 or die ("MySQL-Error: " . mysql_error());
		 while($row = mysql_fetch_object($result2)){
		 	$goalid = $row->id;
		 }
		 if (preg_match('/\b(19|20)?[0-9]{2}[-](0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01])\b/', $deadline)){
			$result3 = mysql_query(
			"INSERT INTO goaldeadline (date, fk_goalid)
			VALUES ('$deadline', '$goalid')")
			or die ("MySQL-Error: " . mysql_error());
			
		 }	
		# add subgoals into database
		
		
		foreach($_POST as $key=>$value) {
			if (startsWith($key, "subgoalname")){
				$splitted = preg_split('/[_]/', $key);
				$subgoalid = $splitted[1];
				
				$tempColumnName = "subgoalname_"."$subgoalid";			
				$subgoalname = $_POST[$tempColumnName];
				$tempColumnName = "subgoalstart_"."$subgoalid";
				$subgoalstartvalue = $_POST[$tempColumnName];
				$tempColumnName = "subgoalcurrent_"."$subgoalid";
				$subgoalcurrentvalue = $_POST[$tempColumnName];
				$tempColumnName = "subgoaltarget_"."$subgoalid" ;
				$subgoaltargetvalue = $_POST[$tempColumnName];
				$tempColumnName = "subgoalpriority_"."$subgoalid";
				$subgoalpriority = $_POST[$tempColumnName];
				$tempColumnName = "subgoalmeasurename_"."$subgoalid";
				$subgoalmeasurename = $_POST[$tempColumnName];
				$tempColumnName = "subgoaldeadline_"."$subgoalid";
				$subgoaldeadline = $_POST[$tempColumnName];
				$tempColumnName = "subgoalmeasuretype_"."$subgoalid";
				$subgoalmeasuretype = $_POST[$tempColumnName];
				
				
				switch ($subgoalpriority) {
	    			case "ui":
	        			$subgoalpriority = "urgent / important";
	        			break;
	    			case "uni":
	        			$subgoalpriority = "urgent / unimportant";
	        			break;
		   			case "nui":
		        			$subgoalpriority = "unurgent / important";
		       			break;
		       		case "nuni":
		        			$subgoalpriority = "unurgent / unimportant";
		       			break;
				}
				
				$result4 = mysql_query("INSERT INTO subgoal (name, startvalue, value, targetvalue, priority, measurename, fk_goalid, fk_measuretypeid)
			 	VALUES ('$subgoalname',
			 	'$subgoalstartvalue',
			 	'$subgoalcurrentvalue',
			 	'$subgoaltargetvalue',
			 	'$subgoalpriority',
			 	'$subgoalmeasurename',
			 	".$goalid.",
			 	".$subgoalmeasuretype.")")
			 	or die ("MySQL-Error: " . mysql_error());
			 	
			 	#get id of just saved subgoal
			 	$subgoalid = "";
			 	$result5 = mysql_query("SELECT id FROM subgoal WHERE fk_goalid=".$goalid." AND name='".$subgoalname."'") or die ("MySQL-Error: " . mysql_error());
				while($row = mysql_fetch_object($result5)){
			 		$subgoalid = $row->id;
			 	}
				if ($subgoaldeadline != ""){
					$result6 = mysql_query("INSERT INTO subgoaldeadline (date, fk_subgoalid) VALUES ('$subgoaldeadline', '$subgoalid')") or die ("MySQL-Error: " . mysql_error());
				}
			}
		}
	}

?>